package com.ygu.mylist.activities;

import com.ygu.mylist.R;
import com.ygu.mylist.service.RemindService;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

public class MainActivity extends FragmentActivity implements OnClickListener{
	private LinearLayout mcurTab,mhisTab;
	private Current_Fragment mcurFm;
	private History_Fragment mhisFm;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		initView();
		selected(0);
		startService(new Intent(this, RemindService.class));
	}
	private void selected(int i) {
		// TODO Auto-generated method stub
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction transaction = fm.beginTransaction();
		hideFragment(transaction);
		switch(i){
		case 0:
			if(mcurFm==null)
			{
				mcurFm=new Current_Fragment();
				transaction.add(R.id.id_content, mcurFm);
			}else{
				transaction.show(mcurFm);
			}
			mcurFm.refreshAdapter();
			break;
		case 1:
			if(mhisFm==null)
			{
				mhisFm=new History_Fragment();
				transaction.add(R.id.id_content, mhisFm);
			}else{
				transaction.show(mhisFm);
			}
			mhisFm.refreshAdapter();
			break;
		}
		transaction.commit();
	}
	private void initView() {
		// TODO Auto-generated method stub
		mcurTab=(LinearLayout) findViewById(R.id.curTab);
		mhisTab=(LinearLayout) findViewById(R.id.hisTab);
		mcurTab.setOnClickListener(this);
		mhisTab.setOnClickListener(this);
		//lv=(ListView) findViewById(R.id.id_lv);
		String draw_string[]={"1","2","3"};
		//lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, draw_string));
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch(v.getId()){
		case R.id.curTab:
			selected(0);
			
			break;
		case R.id.hisTab:
			selected(1);
			break;
		}
		
	}
	private void hideFragment(FragmentTransaction transaction)
	{
		if (mcurFm != null)
		{
			transaction.hide(mcurFm);
		}
		if (mhisFm != null)
		{
			transaction.hide(mhisFm);
		}

	}
}
